/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-09-02
 * Time: 17:08
 */
public class Solution {
    public int[] sortArray(int[] nums) {
        //insertSort(nums);
        //shellSort(nums);
        //selectSort(nums);
        //heapSort(nums);
        //bubbleSort(nums);
        //quickSort(nums);
        mergeSort(nums);
        return nums;
    }

    //归并排序
    public void mergeSort(int[] arr) {
        mergeSortTmp(arr,0,arr.length - 1);
    }
    public void mergeSortTmp(int[] arr,int start,int end) {
        if (start >= end) {
            return;
        }
        //开始分解
        int mid = (start + end)/2;
        mergeSortTmp(arr,start,mid);
        mergeSortTmp(arr,mid + 1,end);

        //开始合并
        merge(arr,start,mid,end);
    }
    public void merge(int[] arr,int left,int mid,int right) {
        int k = 0;
        int[] tmp = new int[right - left + 1];
        int s1 = left;
        int e1 = mid;
        int s2 = mid + 1;
        int e2 = right;
        while (s1 <= e1 && s2 <= e2) {
            if (arr[s1] <= arr[s2]) {
                tmp[k++] = arr[s1++];
            }else {
                tmp[k++] = arr[s2++];
            }
        }
        while (s1 <= e1) {
            tmp[k++] = arr[s1++];
        }

        while (s2 <= e2) {
            tmp[k++] = arr[s2++];
        }

        for (int i = 0; i < k; i++) {
            arr[i + left] = tmp[i];
        }
    }

    //快速排序
    public void quickSort(int[] arr) {
        quick(arr,0,arr.length - 1);
    }
    public void quick(int[] arr,int start,int end) {
        if (start >= end) {
            return;
        }

        int mid = findMiddle(arr,start,end);
        swap(arr,mid,start);

        if (end - start + 1 <= 7) {
            insertRange(arr,start,end);
            return;
        }

        int pivot = partition(arr,start,end);
        //处理左边子数组
        quick(arr,start,pivot - 1);
        //处理右边子数组
        quick(arr,pivot + 1,end);
    }
    //找出分割点
    public int partition(int[] arr,int left,int right) {
        int tmp = arr[left];
        while (left < right) {
            while (left < right && arr[right] >= tmp) {
                right--;
            }
            arr[left] = arr[right];
            while (left < right && arr[left] <= tmp) {
                left++;
            }
            arr[right] = arr[left];
        }
        arr[left] = tmp;
        return left;
    }
    //找出中位数
    public int findMiddle(int[] arr,int left,int right) {
        int mid = (left + right)/2;
        if (arr[left] < arr[right]) {
            if (arr[mid] < arr[left]){
                return left;
            }else if (arr[mid] > arr[right]) {
                return right;
            }else {
                return mid;
            }
        }else {
            if (arr[mid] < arr[right]){
                return right;
            }else if (arr[mid] > arr[left]) {
                return left;
            }else {
                return mid;
            }
        }
    }
    //一定范围内进行插入排序
    public void insertRange(int[] arr,int start,int end) {
        for (int i = start; i <= end; i++) {
            int tmp = arr[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (arr[j] > tmp) {
                    arr[j + 1] = arr[j];
                }else {
                    arr[j + 1] = tmp;
                    break;
                }
            }
            arr[j + 1] = tmp;
        }
    }

    //冒泡排序
    public void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            boolean flag = false;
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    swap(arr,j,j + 1);
                    flag = true;
                }
            }
            if (!flag) {
                break;
            }
        }
    }

    //堆排序
    public void heapSort(int[] arr) {
        //构建大根堆
        for (int i = (arr.length - 1 - 1)/2; i >= 0; i--) {
            siftDown(arr,i,arr.length - 1);
        }

        //进行排序
        int end = arr.length - 1;
        while (end != 0) {
            swap(arr,0,end);
            end--;
            siftDown(arr,0,end);
        }
    }
    public void siftDown(int[] arr,int parent,int length) {
        int child = 2*parent + 1;
        while (child <= length) {
            if (child + 1 <= length && arr[child] < arr[child + 1]) {
                child++;
            }
            if (arr[child] > arr[parent]) {
                swap(arr,child,parent);
                parent = child;
                child = 2*parent + 1;
            }else {
                break;
            }
        }
    }

    //选择排序
    public void selectSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            swap(arr,i,minIndex);
        }
    }

    //交换
    public void swap(int[] arr,int i,int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    //希尔排序
    public void shellSort(int[] arr) {
        int gap = arr.length;
        while (gap > 1) {
            gap /= 2;
            shell(arr,gap);
        }
    }
    public void shell(int[] arr,int gap) {
        for (int i = gap; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (arr[j] > tmp) {
                    arr[j + gap] = arr[j];
                }else {
                    arr[j + gap] = tmp;
                    break;
                }
            }
            arr[j + gap] = tmp;
        }
    }

    //直接插入排序
    public void insertSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (arr[j] > tmp) {
                    arr[j + 1] = arr[j];
                }else {
                    arr[j + 1] = tmp;
                    break;
                }
            }
            arr[j + 1] = tmp;
        }
    }
}
